/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.example.netcracker.ui;

import ru.example.netcracker.ui.listeners.OnClickMenuItemListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 *
 * @author Nikolay
 */
public final class TablePopupMenu extends JPopupMenu implements ActionListener {
    
    private static final String MENU_CHANGE_CMD = "change";
    private static final String MENU_DELETE_CMD = "delete";
    
    private OnClickMenuItemListener mListener;
    
    public TablePopupMenu(OnClickMenuItemListener listener) {
        super();
        this.mListener = listener;
        JMenuItem changeMenu = new JMenuItem("Изменить");
        changeMenu.setActionCommand(MENU_CHANGE_CMD);
        changeMenu.addActionListener(this);
        JMenuItem deleteMenu = new JMenuItem("Удалить");
        deleteMenu.setActionCommand(MENU_DELETE_CMD);
        deleteMenu.addActionListener(this);
        this.add(changeMenu);
        this.add(deleteMenu);
    }
    
    public TablePopupMenu() {
        this(null);
    }
    
    public void addOnClickMenuItemListener(OnClickMenuItemListener listener) {
        mListener = listener;
    }
    
    public void removeOnClickMenuItemListener() {
        mListener = null;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (mListener != null) {
            switch (e.getActionCommand()) {
                case MENU_CHANGE_CMD:
                    mListener.onClickChange();
                    break;
                case MENU_DELETE_CMD:
                    mListener.onClickDelete();
            }
        }
    }
}
